package com.wythe.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;

import com.wythe.mall.BaseApplication;
import com.wythe.mall.R;
import com.wythe.mall.adapter.BaseFragmentAdapter;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.ui.GoodsFragment;

import java.util.ArrayList;
import java.util.List;

import top.yokey.base.base.BaseCountTime;
import top.yokey.base.event.GoodsAreaEvent;
import top.yokey.base.event.GoodsEvaluateEvent;
import top.yokey.base.event.GoodsShowEvent;

public class GoodsActivity extends BaseActivity {

    private Toolbar mainToolbar;
    private ViewPager mainViewPager;
    private AppCompatTextView[] navigationTextView;

    private int goodsIdString;
    private boolean isShowBoolean;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_goods);
        goodsIdString = getIntent().getExtras().getInt("id");
        initView();
        initData();
        initEven();
    }

    public void initView() {

        mainToolbar = (Toolbar) findViewById(R.id.mainToolbar);
        mainViewPager = (ViewPager) findViewById(R.id.mainViewPager);
        AppCompatTextView goodsTextView = (AppCompatTextView) findViewById(R.id.goodsTextView);
        navigationTextView = new AppCompatTextView[1];
        navigationTextView[0] = goodsTextView;

    }

    public void initData() {

        List<Fragment> fragmentList = new ArrayList<>();
        GoodsFragment myFragment = new GoodsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id",goodsIdString);
        myFragment.setArguments(bundle);
        fragmentList.add(myFragment);

        mainViewPager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList));
        mainViewPager.setOffscreenPageLimit(navigationTextView.length);
        isShowBoolean = false;

        setToolbar(mainToolbar, "");
        updateNavigation(0);
        getGoodsInfo();

    }

    public void initEven() {

        for (int i = 0; i < navigationTextView.length; i++) {
            final int position = i;
            navigationTextView[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateNavigation(position);
                }
            });
        }

        mainViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateNavigation(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onReturn() {

        if (mainViewPager.getCurrentItem() != 0) {
            mainViewPager.setCurrentItem(0);
            return;
        }

        if (isShowBoolean) {
            return;
        }

        if (BaseApplication.get().inActivityStackTop() && !BaseApplication.get().inActivityStack(MainActivity.class)) {
            BaseApplication.get().start(getActivity(), MainActivity.class);
        }

        super.onReturn();

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            onReturn();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onActivityResult(int req, int res, Intent intent) {
        super.onActivityResult(req, res, intent);
        if (res == RESULT_OK) {
            if (req == 1000) {
                final GoodsAreaEvent goodsAreaEvent = new GoodsAreaEvent(intent.getStringExtra("area_id"), intent.getStringExtra("area_info"));
                new BaseCountTime(500, 250) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                }.start();
            }
        }
    }

    //自定义方法

    private void getGoodsInfo() {

    }

    private void updateNavigation(int position) {

//        for (AppCompatTextView appCompatTextView : navigationTextView) {
//            appCompatTextView.setTextColor(this.getResources().getColor(R.color.grey));
//        }
//
//        navigationTextView[position].setTextColor(this.getResources().getColor(R.color.primary));
        mainViewPager.setCurrentItem(position);

    }

    public void onGoodsShowEvent(GoodsShowEvent event) {

        isShowBoolean = event.isShow();

    }

    public void onGoodsEvaluateEvent(GoodsEvaluateEvent event) {

        mainViewPager.setCurrentItem(2);

    }

}
